package pl.ptemich.tankwars.objects;

import org.newdawn.slick.Image;

import pl.ptemich.tankwars.GraphicConstants;

public abstract class PhysicalObject implements GameObject {

  protected Float posX;
  
  protected Float posY;
  
  protected Integer width;
  
  protected Integer height;
  
  private Boolean visible;
  
  private Image view;
  
  private Boolean selected;
  
  public PhysicalObject(Image view) {
    this.view = view;
    this.width = view.getWidth();
    this.height = view.getHeight();    
    init();
  }
    
  
  public void init() {
    randomPosition();    
    visible = false;
    selected = false;
  }
  
  public void draw() {
    if (visible) {
      if (selected) {
        GraphicConstants.SELECTION_IMAGE.draw(posX, posY);
      }
      
      view.draw(posX, posY);
    }
  }
  
  
  public void randomPosition() {
    posX = Double.valueOf(Math.random()).floatValue() * GraphicConstants.SCENE_WIDTH;
    
    if (posX + width > GraphicConstants.SCENE_WIDTH) {
      posX = (float) GraphicConstants.SCENE_WIDTH - width;
    }
    
    
    posY = Double.valueOf(Math.random()).floatValue() * GraphicConstants.SCENE_HEIGHT;
    
    if (posY + height > GraphicConstants.SCENE_HEIGHT) {
      posY = (float) GraphicConstants.SCENE_HEIGHT - height;
    }
        
  }


  public void setVisible(Boolean visible) {
    this.visible = visible;
  }


  public Boolean isSelected() {
    return selected;
  }


  public void setSelected(Boolean selected) {
    this.selected = selected;
  }


  public Boolean isVisible() {
    return visible;
  }
  
  public Boolean checkSelection(Integer clickX, Integer clickY) {    
    selected = false;
    if (posX < clickX && (posX + width > clickX)) {
      if (posY < clickY && (posY + height > clickY)) {
        selected = true;
      }
    }
    return selected;
  }
  
}
